let layer=null;
let laytpl=null,upload=null;
let getTpl = imageListTpl.innerHTML
    ,view = document.getElementById('imageList');
let params= {type:'CAROUSEL'};
let publishCarouselImage=function(publicInfoId){
    eyc.ajax.promise('/eyc/publishCarouselImage',publicInfoId).then(res=>{
        if(res.data){
            refreshCarouselImage({method: 'GET',pageCde:params.pageCde,typeCde:'CAROUSEL'});
            layer.msg('上架成功');
        }
    })
};
let pullOffCarouselImage=function(publicInfoId){
    eyc.ajax.promise('/eyc/pullOffCarouselImage',publicInfoId).then(res=>{
        if(res.data){
            refreshCarouselImage({method: 'GET',pageCde:params.pageCde,typeCde:'CAROUSEL'});
            layer.msg('下架成功');
        }
    })
};
let deleteCarouselImage=function(publicInfoId){
    eyc.ajax.promise('/eyc/deleteCarouselImage',publicInfoId).then(res=>{
        if(res.data){
            refreshCarouselImage({method: 'GET',pageCde:params.pageCde,typeCde:'CAROUSEL'});
            layer.msg('删除成功');
        }
    })
};
let refreshCarouselImage=function(params){
    eyc.ajax.promise('/eyc/searchPageConfigByTypeCde',params).then(res => {
        let publicInfos=res.data;
        if(publicInfos){
            laytpl(getTpl).render(publicInfos, function(html){
                view.innerHTML = html;
                uploadFile(publicInfos);
            });
        }
    });
};
let config = {
    $tree:$('#treeConfigType'),
    $rMenu:$("#rMenu")
};
let calcTableHeight = ($tableContainer) => {
    var paddingTop = $('body').css('padding-top');
    var bodyPaddingHeight = paddingTop.replace(/[^0-9]/ig, "");
    var height = $(window).height() - $tableContainer.offset().top - $(window).scrollTop() - bodyPaddingHeight;
    return height;
};


let uploadFile=(publicInfos)=>{
    upload.render({
        elem: '#uploadImage' //绑定元素
        ,url: '/file/uploadFile' //上传接口
        ,data:params
        ,done: function(res){
            if(res.data){
                publicInfos.push(res.data);
                laytpl(getTpl).render(publicInfos, function(html) {
                    view.innerHTML = html;
                    uploadFile(publicInfos);
                });
            }
        }
        ,error: function(){
            //请求异常回调
        }
    });
};

let initConfigTypeTree=(data)=>{
    let setting = {
        check : {
            enable : false,
            chkboxType : {
                "Y" : "ps",
                "N" : "s"
            }
        },
        data : {
            simpleData: {
                enable:true
            },
            key : {
                name : "pageNme"
            }
        },
        edit : {
            enable : false,
        },
        callback : {
            onClick : 	function(event, treeId, treeNode){
                params.pageCde=treeNode.pageCde;
                params.pageNme=treeNode.pageNme;
                refreshCarouselImage({method: 'GET',pageCde:treeNode.pageCde,typeCde:'CAROUSEL'});
            },
            onRightClick: function(event, treeId, treeNode) {
                let level = -1;
                if(treeNode)
                    level = treeNode.level;
                if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
                    config.$tree.cancelSelectedNode();
                    showRMenu(level, event.clientX, event.clientY);
                } else if (treeNode && !treeNode.noR) {
                    config.$tree.selectNode(treeNode);
                    showRMenu(level, event.clientX, event.clientY, treeNode);
                }
            }
        },
        view: {
            fontCss: function(treeId, treeNode) {
                return treeNode.isActive ? {} : { color: "#FF5722" };
            }
        }

    };
    let blankNode = {id : "blank", pageNme:"页面", open:true, isActive:true, children:data};
    let typeNodes = [blankNode];
    config.$tree=$.fn.zTree.init(config.$tree, setting, typeNodes);
    let nodes =   config.$tree.getNodes();
    config.$tree.checkNode(nodes[0].children[0], true, true);
    //获取第一个节点
    let node =   config.$tree.getNodes()[0].children[0];
    config.$tree.selectNode(node);
    setting.callback.onClick(null,   config.$tree.setting.treeId, node);
};
let showRMenu = (leve, x, y, treeNode) => {
    $("#rMenu ul").show();
    if (leve == -1) {
        return;
    } else {
        if (leve == 0) {
            $("#m_add").show();
            $("#m_edit").hide();
            $("#m_active").hide();
            $("#m_inActive").hide();
        } else {
            $("#m_add").hide();
            if (treeNode.isActive) {
                $("#m_active").hide();
                $("#m_inActive").show();
            } else {
                $("#m_active").show();
                $("#m_inActive").hide();
            }
            $("#m_edit").show();
        }
    }
    y += document.body.scrollTop;
    x += document.body.scrollLeft;
    config.$rMenu.css({"top": y + "px", "left": x + "px", "visibility": "visible"});
    $("body").bind("mousedown", onBodyMouseDown);
};
let hideRMenu=()=> {
    if (config.$rMenu) config.$rMenu.css({"visibility": "hidden"});
    $("body").unbind("mousedown", onBodyMouseDown);
};

let onBodyMouseDown=(event)=>{
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
        config.$rMenu.css({"visibility" : "hidden"});
    }
};
$(function () {
    layui.use(['layer','laytpl','upload'], function () {
        layer=layui.layer;
        laytpl =  layui.laytpl;
        upload = layui.upload;
    });
    eyc.ajax.promise('/eyc/searchPublishInfoPageByTypeCde', 'CAROUSEL',false).then(res => {
        initConfigTypeTree(res.data);
    });

});
